﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using Dtf.Core.Remoting;

namespace Dtf.Core
{
    public class LogManager : RemotingObject
    {
        public static void Write(string log)
        {
            Write(log, "white");
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="log"></param>
        /// <param name="color">
        /// FOREGROUND_BLUE      0x0001 // text color contains blue.                
        /// FOREGROUND_GREEN     0x0002 // text color contains green.                 
        /// FOREGROUND_RED       0x0004 // text color contains red.
        /// FOREGROUND_INTENSITY 0x0008 // text color is intensified.
        /// BACKGROUND_BLUE      0x0010 // background color contains blue.
        /// BACKGROUND_GREEN     0x0020 // background color contains green.
        /// BACKGROUND_RED       0x0040 // background color contains red.
        /// BACKGROUND_INTENSITY 0x0080 // background color is intensified.</param>
        public static void Write(string log, string color)
        {
            short colorId = 0;
            color = color.ToLower();
            switch (color)
            {
                case "white":
                    colorId = 0x4 | 0x2 | 0x1;
                    break;
                case "red":
                    colorId = 0x4;
                    break;
                case "green":
                    colorId = 0x2;
                    break;
                case "blue":
                    colorId = 0x1;
                    break;
                case "intensified":
                    colorId = 0x8;
                    break;
            }

            IntPtr hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

            //if (!_HasInitialized)
            //{
            //    CONSOLE_SCREEN_BUFFER_INFO screenInfo = new CONSOLE_SCREEN_BUFFER_INFO();

            //    if (!GetConsoleScreenBufferInfo(hConsole, out screenInfo))
            //    {
            //        return;
            //    }
            //    int nLen = screenInfo.dwSize.X * screenInfo.dwSize.Y;

            //    int nWritten = 0;
            //    COORD coord = new COORD();
            //    coord.X = 0;
            //    coord.Y = 0;
            //    //FillConsoleOutputAttribute(hConsole, CONSOLE_BACKGROUND, nLen, coord, out nWritten);
            //}

            SetConsoleTextAttribute(hConsole, (short)(colorId));
            Console.WriteLine(log);
            SetConsoleTextAttribute(hConsole, (short)(0x4|0x2|0x1));    //white text
        }



        [DllImportAttribute("Kernel32.dll")]
        private static extern IntPtr GetStdHandle(int nStdHandle);

        [DllImportAttribute("Kernel32.dll")]
        private static extern bool SetConsoleTextAttribute(IntPtr hConsoleOutput, short wAttributes);

        /*[DllImport("Kernel32.dll")]
        [return: MarshalAs(UnmanagedType.U1)]
        private static extern bool FillConsoleOutputAttribute(IntPtr hConsoleOutput, short wAttribute, int nLength, COORD dwWriteCoord, out int lpNumberOfAttrsWritten);

        [DllImport("Kernel32.dll")]
        [return: MarshalAs(UnmanagedType.U1)]
        private static extern bool GetConsoleScreenBufferInfo(IntPtr hConsoleOutput, out CONSOLE_SCREEN_BUFFER_INFO lpConsoleScreenBufferInfo);
        */

        /*struct COORD
        {
            public short X;
            public short Y;
        }
        struct SMALL_RECT
        {
            public short Left;
            public short Top;
            public short Right;
            public short Bottom;

        }
        struct CONSOLE_SCREEN_BUFFER_INFO
        {
            public COORD dwSize;
            public COORD dwCursorPosition;
            public short wAttributes;
            public SMALL_RECT srWindow;
            public COORD dwMaximumWindowSize;
        }
        */

        private const int STD_OUTPUT_HANDLE = -11;
        private const short CONSOLE_BACKGROUND = 0x0080;
        //private static bool _HasInitialized = false;



    }
}
